#include <stdio.h>
#include "platform.h"
#include "flash_app.h"

#define FLASH_START_ADDRESS     ((uint32_t)0x08000000)
#define FLASH_PAGE_SIZE         (1024)

DataStore data_store;

uint8_t flash_init(void)
{
    uint32_t OffsetAddress = FLASH_START_ADDRESS + 63 * FLASH_PAGE_SIZE;
    // memcpy((char*)data_store,(cahr*)OffsetAddress,sizeof(DataStore));
}

void flash_write()
{
	uint32_t WriteValue    = 0x12345678;
    uint32_t ReadValue     = 0;
    uint32_t OffsetAddress = FLASH_START_ADDRESS + 63 * FLASH_PAGE_SIZE;

    FLASH_Unlock();

    FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);

    FLASH_ErasePage(OffsetAddress);
    FLASH_ClearFlag(FLASH_FLAG_EOP);

    FLASH_ProgramWord(OffsetAddress, WriteValue);
    FLASH_ClearFlag(FLASH_FLAG_EOP);

    FLASH_Lock();

    ReadValue = *(volatile uint32_t *)(OffsetAddress);

    printf("\r\nRead  Value : 0x%08x", ReadValue);

    if (ReadValue == WriteValue)
    {
        printf("\t-->Passed");
        
    }
    else
    {
        printf("\t-->Failed");
        
    }
}